import matplotlib.pyplot as plt


def readFromCsv(fileName):
    fo = open(fileName)
    lst = []
    for line in fo:
        if line != '':
            line = line.replace('\n', '')
            lst.append(line.split(','))
    fo.close()
    return lst


lst = readFromCsv(r'record.csv')
dicts = {}
NIND = lst[0][0]  # 种群大小
MAXGEN = lst[0][1]   # 迭代最大代数
Pm = lst[0][3]   # 变异概率

for i in range(len(lst)):
    key = eval(lst[i][2])
    value = eval(lst[i][-1])
    if key in dicts.keys():
        if value < dicts[key]:
            dicts[key] = value
    else:
        dicts[key] = value

dicts = dict(sorted(dicts.items(), key=lambda d:d[0]))
print(min(dicts, key=lambda x : dicts[x]))
plt.rcParams['font.sans-serif'] = ['SimHei']  # 显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 显示负号
plt.xlabel('交叉概率Pm')
plt.ylabel('最优解对应的值')
plt.title("种群大小"+str(NIND)+'迭代最大次数'+str(MAXGEN)+'变异概率'+str(Pm))
plt.plot(dicts.keys(), dicts.values())
plt.show()




